package com.zl.registration.service.impl;

import com.zl.registration.dao.UserInfoDao;
import com.zl.registration.dao.UserSecurityDao;
import com.zl.registration.entity.UserSecurityQuestion;
import com.zl.registration.module.prefix.UserKey;
import com.zl.registration.service.RedisService;
import com.zl.registration.service.UserRetrieveService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zoulei
 * @date 2021/11/26 10:52
 */
@Service
public class UserRetrieveServiceImpl implements UserRetrieveService {

    @Resource
    private UserSecurityDao userSecurityDao;

    @Resource
    private UserInfoDao userInfoDao;

    @Resource
    private RedisService redisService;

    @Override
    public String findQuestionByPhone(String phone) {
        UserSecurityQuestion questionByPhone = userSecurityDao.searchByPhone(phone);
        //将答案存储在redis中
        redisService.set(UserKey.ANSWER_BY_PHONE, phone, questionByPhone.getAnswer());
        //返回问题
        return questionByPhone.getQuestion();
    }

    @Override
    public Boolean checkAnswer(String phone, String answer) {
        String realAnswer = redisService.get(UserKey.ANSWER_BY_PHONE, String.valueOf(phone));
        return realAnswer.equals(answer);
    }

    @Override
    public Boolean resetPassword(String phone, String password) {
        return userInfoDao.resetPassword(phone, password) == 1;
    }

}
